package com.yugao.fintech.antelope.system.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.yugao.fintech.antelope.base.model.BasePlusPO;
import com.yugao.fintech.antelope.base.model.constants.UserCons;
import com.yugao.fintech.antelope.base.model.module.dict.CusFieldBind;
import com.yugao.fintech.antelope.base.model.module.dict.DictTypeCons;
import com.yugao.fintech.framework.core.annotations.FieldUnique;
import com.yugao.fintech.framework.core.annotations.FieldUniqueConfig;
import com.yugao.fintech.framework.core.enums.SqlConditionTypeEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@TableName("sys_user")
@Getter
@Setter
@Accessors(chain = true)
@FieldUniqueConfig(tableId = "user_id", entityId = "userId", sqlCondition = SqlConditionTypeEnum.OR)
public class SysUser extends BasePlusPO {
    private static final long serialVersionUID = 1L;

    /** 用户ID */
    @TableId
    private Long userId;

    /** 部门ID */
    private Long deptId;

    /** 工号 */
    private String workNumber;

    /** 当前的部门id */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long currentDeptId;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long currentTenantId;

    /** 用户账号 */
    @FieldUnique(message = "用户名已存在")
    private String userName;

    /** 用户真实姓名 */
    private String realName;

    /** 用户昵称 */
    private String nickName;

    /** 用户邮箱 */
    @FieldUnique(message = "邮箱已存在")
    private String email;

    /** 手机号码 */
    @FieldUnique(message = "手机号已存在")
    private String phone;

    /**
     * 用户性别, 0=男,1=女,2=未知
     * @see UserCons
     */
    @CusFieldBind(type = DictTypeCons.USER_SEX, target = "sexText")
    private String sex;

    /** 绑定显示属性，非表字典（排除） */
    @TableField(exist = false)
    private String sexText;

    /** 用户头像 */
    private String avatar;

    /**
     * 帐号状态
     * @see UserCons
     */
    @CusFieldBind(type = DictTypeCons.USER_STATUS, target = "statusText")
    private String status;

    /** 绑定显示属性，非表字典（排除） */
    @TableField(exist = false)
    private String statusText;

    /** 删除标志 */
    @FieldUnique(condition = FieldUnique.Condition.NE, value = "1")
    private String delFlag;

    /** 最后登录IP */
    private String lastLoginIp;

    /** 最后登录时间 */
    private Date lastLoginDate;

    /**
     * 用户来源类型
     * @see UserSourceEnum
     */
    private String userSourceType;

    /**
     * 如果是自行注册的用户, 则表示租户id
     */
    private String userSourceId;

    /**
     * 用户生日
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date birthday;

    /**
     * 用户来源,1-后台管理员, 2-租户用户
     * @see UserCons
     */
    @FieldUnique(condition = FieldUnique.Condition.EQ)
    private String source;

    /** 部门对象 */
    @TableField(exist = false)
    private List<SysDept> depts;

    /** 部门ids */
    @TableField(exist = false)
    private List<Long> deptIds;

    /** 角色组 */
    @TableField(exist = false)
    private List<Long> roleIds;

    /**
     * 租户id
     */
    @TableField(exist = false)
    private Long tenantId;

    /**
     * 当前用户被添加到某个租户的时间, sql关联查询会用到
     */
    @TableField(exist = false)
    private Date addTime;


    public SysUser() {

    }

    public boolean isAdmin() {
        return isAdmin(roleIds);
    }

    public static boolean isAdmin(List<Long> roleIds) {
        return roleIds.stream().filter(Objects::nonNull)
                .anyMatch(e -> UserCons.Role.ADMIN_ID.compareTo(e) == 0);
    }


    public static LambdaQueryWrapper<SysUser> lqw() {
        return Wrappers.lambdaQuery();
    }

    public static LambdaUpdateWrapper<SysUser> luw() {
        return Wrappers.lambdaUpdate();
    }
}
